package com.Arsanesia.TempleRush.Views.Screens;

import com.Arsanesia.TempleRush.TempleRush;
import com.Arsanesia.TempleRush.Controllers.World;
import com.Arsanesia.TempleRush.Controllers.Listeners.GameTapListener;
import com.Arsanesia.TempleRush.Controllers.Listeners.ButtonListeners.GamePauseListener;
import com.Arsanesia.TempleRush.Controllers.Listeners.ButtonListeners.PauseHomeListener;
import com.Arsanesia.TempleRush.Controllers.Listeners.ButtonListeners.SoundConfigurationListener;
import com.Arsanesia.TempleRush.Controllers.Listeners.ButtonListeners.VibrationConfigurationListener;
import com.Arsanesia.TempleRush.Enumeration.TempleRushMusic;
import com.Arsanesia.TempleRush.Models.GameConfiguration;
import com.Arsanesia.TempleRush.Models.Profile;
import com.Arsanesia.TempleRush.Views.Components.Buttons.GamePausedButton;
import com.Arsanesia.TempleRush.Views.Screens.Components.PopUp.QuitAttemptPopup;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;


public class GameScreen extends AbstractScreen{
	private World world;
	private GamePausedButton pausebutton;
	private Image pausehomebutton;
	private ImageButton pausevibratebutton;
	private ImageButton pausesoundbutton;
	private Color defaultStageColor;
	private QuitAttemptPopup quitAttempt;
	private Profile profile;	

	
	public GameScreen(TempleRush game){
		super(game);
	}
	@Override
	public void show(){
		super.show();	
		//retrieve the profile
		profile = game.getProfileManager().retrieveProfile();
		//set up texture atlas
		atlas = new TextureAtlas(Gdx.files.internal("Images/Game/game.atlas"));
		//create the world
		world = new World(atlas);
		//create the quitattempt image
		quitAttempt = new QuitAttemptPopup(atlas,game,world);
		
		//create pausehomebutton
		TextureRegion pausehomeregion = atlas.createSprite("pausehome");
		pausehomebutton = new Image(new TextureRegionDrawable(new TextureRegion(pausehomeregion)));
		//make pausehomebutton invisible
		pausehomebutton.addAction(Actions.alpha(0));
		pausehomebutton.addListener(new PauseHomeListener(quitAttempt));
		
		//create pausevibratebutton and make invisible
		Array<Sprite> pausevibratesprites = atlas.createSprites("vibrate");
		pausevibratebutton = new ImageButton(new TextureRegionDrawable(new TextureRegion(pausevibratesprites.get(0))),
				 new TextureRegionDrawable(new TextureRegion(pausevibratesprites.get(0))),
				 new TextureRegionDrawable(new TextureRegion(pausevibratesprites.get(1))));
		pausevibratebutton.addAction(Actions.alpha(0));
		pausevibratebutton.setChecked(!GameConfiguration.isVibrate());
		pausevibratebutton.addListener(new VibrationConfigurationListener());
		
		//create pausesoundbutton and make invisible
		Array<Sprite> pausesoundsprites = atlas.createSprites("sound");
		pausesoundbutton = new ImageButton(new TextureRegionDrawable(new TextureRegion(pausesoundsprites.get(0))),
				 new TextureRegionDrawable(new TextureRegion(pausesoundsprites.get(0))),
				 new TextureRegionDrawable(new TextureRegion(pausesoundsprites.get(1))));
		pausesoundbutton.addAction(Actions.alpha(0));
		pausesoundbutton.setChecked(!GameConfiguration.isSoundEnabled());
		//add sound listener
		pausesoundbutton.addListener(new SoundConfigurationListener());
		//create pause button
		Array<Sprite> pauseButtonSprites = atlas.createSprites("pause");
		
		pausebutton = new GamePausedButton( new TextureRegionDrawable(new TextureRegion(pauseButtonSprites.get(0))),
											new TextureRegionDrawable(new TextureRegion(pauseButtonSprites.get(1))),
											new TextureRegionDrawable(new TextureRegion(pauseButtonSprites.get(2))));
		
		pausebutton.addListener(new GamePauseListener(world,pausehomebutton,pausesoundbutton, pausevibratebutton,pausebutton.getWidth(),pausebutton.getHeight()));
		
		//add components to stage
		world.addInternalComponents(this);
		this.addActor(pausehomebutton);
		this.addActor(pausesoundbutton);
		this.addActor(pausevibratebutton);
		this.addActor(pausebutton);
		this.addActor(quitAttempt.getQuitImage());
		this.addListener(new GameTapListener(world));
		defaultStageColor = new Color(this.getRoot().getColor());
		
		//play the music
		GameConfiguration.musicmanager.play(TempleRushMusic.PLAY);
		
		
	}
	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
	}
	
	@Override
	public void render(float delta){
		super.render(delta);
	
		//update when ready
		if(world.isWorldReady()){
			world.update();
			this.getRoot().setColor(new Color(defaultStageColor));
			
			//make all sub pause button invisible
			pausehomebutton.addAction(Actions.alpha(0));
			pausesoundbutton.addAction(Actions.alpha(0));
			pausevibratebutton.addAction(Actions.alpha(0));
			quitAttempt.setQuitInvisible();
			
			//draw score and level only when not paused
			this.getSpriteBatch().begin();
	        font.draw(this.getSpriteBatch(), "Level", VIEWPORT_DEFAULTWIDTH/3, 2* VIEWPORT_DEFAULTHEIGHT/16);
			font.draw(this.getSpriteBatch(), world.getLevel()+"", VIEWPORT_DEFAULTWIDTH/3,  VIEWPORT_DEFAULTHEIGHT/16);
	        font.draw(this.getSpriteBatch(), "Score", 2*VIEWPORT_DEFAULTWIDTH/3, 2* VIEWPORT_DEFAULTHEIGHT/16);
			font.draw(this.getSpriteBatch(), world.getScore()+"", 2*VIEWPORT_DEFAULTWIDTH/3,  VIEWPORT_DEFAULTHEIGHT/16);
			if(world.isSmoke())
				world.getSmoke().getSmokeBg().draw(this.getSpriteBatch(), 1);
			this.getSpriteBatch().end();

		}
		else if(world.isTutorMode()){
			world.startTutorMode();
			//set paused button to be play
			pausebutton.setChecked(true);
			this.getSpriteBatch().begin();
			world.drawTutorial(this.getSpriteBatch(),font);
			pausebutton.draw(this.getSpriteBatch(), 1);
			this.getSpriteBatch().end();
			
		}else if(!world.isGameOver()){
			//draw black screen with half opacity
			//Gdx.gl.glClearColor( 0f, 0f, 0f, 0.5f );
	        //Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
			//make all sub pause button visible
			pausehomebutton.addAction(Actions.alpha(1));
			pausesoundbutton.addAction(Actions.alpha(1));
			pausevibratebutton.addAction(Actions.alpha(1));
			
			this.getRoot().setColor(new Color(0,0,0,0.3f));
	        TextBounds textBounds = font.getBounds("GAME PAUSED");
	        this.getSpriteBatch().begin();
	        font.draw(this.getSpriteBatch(), "GAME PAUSED", 
	        		  (VIEWPORT_DEFAULTWIDTH - textBounds.width )/2, (VIEWPORT_DEFAULTHEIGHT - textBounds.height)/2);
	        //redraw the pause subbutton
	        pausebutton.draw(this.getSpriteBatch(), 1);
	        pausesoundbutton.draw(this.getSpriteBatch(), 1);
	        pausehomebutton.draw(this.getSpriteBatch(), 1);
	        pausevibratebutton.draw(this.getSpriteBatch(), 1);
	        if(quitAttempt.canBeDrawn()){
	        	quitAttempt.getQuitImage().draw(this.getSpriteBatch(), 1);	
	        	
	        }
	        this.getSpriteBatch().end();
		}else if(world.isGameOver()){
			//update the profiles
        	profile.updateProfile(world.getScore(), world.getLevel(), world.getTapCount());
        	profile.updateTappedGeenieType(world.getTappedGeenieCount());
        	//jump to gameover screen
			game.setScreen(game.getGameOverScreen());
		}
        
	}
	@Override
	public boolean keyDown(int keycode){
		if(keycode == Keys.BACK){
			world.togglePaused();
			quitAttempt.setQuitVisible();
			pausebutton.setChecked(!pausebutton.isChecked());
			float buttonWidth = pausebutton.getWidth();
			float buttonHeight = pausebutton.getHeight();
			if(world.isWorldReady()){
				pausehomebutton.addAction(Actions.moveTo(0, 0, 0.1f));
				pausevibratebutton.addAction(Actions.moveTo(0, 0, 0.1f));
				pausesoundbutton.addAction(Actions.moveTo(0, 0, 0.1f));
			}
			else{
				pausehomebutton.addAction(Actions.moveTo(buttonWidth+pausehomebutton.getWidth(),0, 0.5f));
				pausesoundbutton.addAction(Actions.moveTo(buttonWidth+pausehomebutton.getWidth()-10,buttonHeight+pausevibratebutton.getHeight()-10, 0.5f));
				pausevibratebutton.addAction(Actions.moveTo(0,buttonHeight+pausevibratebutton.getHeight(), 0.5f));
				
				
			}
		}
		return false;
	}
}
